AdmiralQ Posted October 11 Share Posted October 11 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/324760-num_rows/ Share on other sites More sharing options...
requinix Posted October 11 Share Posted October 11 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. Quote Link to comment https://forums.phpfreaks.com/topic/324760-num_rows/#findComment-1637389 Share on other sites More sharing options...
AdmiralQ Posted October 12 Author Share Posted October 12 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. Quote Link to comment https://forums.phpfreaks.com/topic/324760-num_rows/#findComment-1637394 Share on other sites More sharing options...
mac_gyver Posted October 12 Share Posted October 12 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>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/324760-num_rows/#findComment-1637396 Share on other sites More sharing options...
AdmiralQ Posted October 12 Author Share Posted October 12 What do you mean “would I accept that from my students?” Who do you think you are talking to me like that? You aren’t my teacher! If you can’t answer without demeaning then don’t answer. Quote Link to comment https://forums.phpfreaks.com/topic/324760-num_rows/#findComment-1637398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.