Jump to content
Larry Ullman's Book Forums

Recommended Posts

I have been updating a website with the scripting from this book. A piece of code I have is not working with the new php and I have reworked this several times.

The following is the error message that I get in the server error log -

PHP Fatal error:  Can't use function return value in write context in 
/hermes/bosnacweb04/bosnacweb04aj/b717/nf.xxxxxxxxx/public_html/xxxxxxxx.com/Login.php on line 137

Line 37 would be the following -  if (mysqli_num_rows($r) = $username) {

 

<?php // Show the user info or the login form:
 if (isset($_SESSION['user_id'])) {
// Show basic user options:

$q = "SELECT user_id, username FROM users WHERE user_id={$_SESSION['user_id']}";

$r = mysqli_query ($db, $q);

//if (mysqli_num_rows($r) > 0) {
    if (mysqli_num_rows($r) = $username) {

//while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
    while ($r = mysqli_fetch_array($r, MYSQLI_NUM)) {
    
// Display the username and heading
        
echo "<p><h2a>Hello $username!</p>";
}
}

 

Thanks

 

 

 

Link to comment
Share on other sites

2 hours ago, Marie said:

I have been updating a website with the scripting from this book. A piece of code I have is not working with the new php and I have reworked this several times.

The following is the error message that I get in the server error log -


PHP Fatal error:  Can't use function return value in write context in 
/hermes/bosnacweb04/bosnacweb04aj/b717/nf.xxxxxxxxx/public_html/xxxxxxxx.com/Login.php on line 137

Line 137 would be the following -  if (mysqli_num_rows($r) = $username) {

 

<?php // Show the user info or the login form:
 if (isset($_SESSION['user_id'])) {
// Show basic user options:

$q = "SELECT user_id, username FROM users WHERE user_id={$_SESSION['user_id']}";

$r = mysqli_query ($db, $q);

//if (mysqli_num_rows($r) > 0) {
    if (mysqli_num_rows($r) = $username) {

//while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
    while ($r = mysqli_fetch_array($r, MYSQLI_NUM)) {
    
// Display the username and heading
        
echo "<p><h2a>Hello $username!</p>";
}
}

 

Thanks

 

 

 

 

Link to comment
Share on other sites

Sorry about the double posting above. I was hoping to delete my original post so I could correct it.

The error message refers to line 137 not 37. Anyway, the original code I used came from the first edition of the eCommerce book which uses "rows". So the original code would have written <p><h2>Hello $row[1]!</p>.

Link to comment
Share on other sites

I think you're missing an equals sign. This code--

if (mysqli_num_rows($r) = $username) {

--is trying to assign the value of $username to mysqli_num_rows($r), which of course isn't possible. You want == $username instead. But that still won't work because mysqli_num_rows() is going to return an array and what you need to do is compare an element in that array to $username.

Link to comment
Share on other sites

  • 3 weeks later...
 Share

×
×
  • Create New...