Jump to content

num_rows


AdmiralQ

Recommended Posts

I am updating from PHP 5 and MySQL 5 and have gotten the hang of basic queries for INSERT, UPDATE, SELECT, and DELETE. But I cannot seem to get num_rows for work. I’ve looked at several sites for examples, but I can’t seem to make it functional. Below is how I would have written a mysql_num_rows() in the past. I know that it’s been deprecated. I would appreciate it if somebody could show me the current re-write. I also know num_rows seems to be frowned upon but I’ve already got my work cut out for me on the upgrades I have to finish before Monday, Oct 21st,  for my students so I’m just needing to understand the current structure. THANK YOU!

$query = "SELECT * FROM ember_students WHERE teacher = '$uname'";
$result= $pdo->query($query);
$num = mysql_num_rows($result);
foreach($result AS $row){
   print $row['lname'].", ".$row['fname']."<br>";
}

 

Link to comment
Share on other sites

You're mixing PDO and mysql. Which is bad for two reasons: you're mixing two different libraries together (so of course they won't work) and one of them is the old mysql library (which is so bad it's been removed from PHP).

You're using PDO. That's good. Find out what the "num rows" solution for PDO is. Hint: look in the documentation to see if anything seems relevant.

Link to comment
Share on other sites

I’m aware I’m using the old mysql. That’s my point. I’m trying to update it. I’ve got a site with 1000+ files and most files have several hundred lines of code. I’ve looked through the documentation for help with syntax and format with this, but I do not understand the way they write it. I always have to google other people’s code to get a complete idea of syntax and format, but this one still eludes me. I just need a simple hand here for updating this one line of code.

Link to comment
Share on other sites

you are trying to pattern match to convert one language/syntax to another. do you accept that from your students, or do you actually expect them to learn the meaning of the words and syntax you are trying to teach them? writing code is no different than a writing assignment in a foreign language. 

the pdo rowCount() method is not guaranteed to work with a SELECT query. as has already been written in your replies, simply fetch the data from a query into an appropriately named php variable, then test if there is any data in the variable. if there isn't, display an appropriate message. if there is, loop over the data to produce the output. here's a pattern for you to use -

// the database specific code, that knows how to query for and fetch the data

// a prepared query, with a place-holder where the dynamic value will be used
$sql = "SELECT * FROM ember_students WHERE teacher = ?";
// prepare the query
$stmt = $pdo->prepare($sql);
// execute the query with an array of the dynamic value(s) you removed from the sql query statement
$stmt->execute([$uname]);
// fetch all the data from the query
$result= $stmt->fetchAll();


// the presentation code, that knows how to produce the output from the data
if(!$result)
{
	echo "<p>There is no data to display.</p>";
}
else
{
	foreach($result AS $row)
	{
		// you can put php variables into a double-quoted php string. no need for error-prone extra quotes and concatenation dots
		print "{$row['lname']}, {$row['fname']}<br>";
	}
}

 

Link to comment
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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.