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

 

 

 

Share this post


Link to post
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

 

 

 

 

Share this post


Link to post
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>.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

I also noticed you have an extra "a" in the h2 tag and you need to close this tag.

echo "<p><h2a>Hello $username!</p>";

should be:

echo "<p><h2>Hello $username!</h2></p>";

Edited by MelissaMMDP

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...