Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Equivalent of this php msql_connect into PDO?
I'm changing from php msql_* into PDO and I need to replace this code:
<?php
if (isset($_GET['username'])){
$username = $_GET['username'];
mysql_connect("localhost","root","") or die ("Could not connect to the server");
mysql_select_db("users") or die ("That database could not be found!");
$userquery = mysql_query("SELECT * FROM users WHERE username='$username'") or die ("The query could not be completed");
if (mysql_num_rows($userquery) != 1){
die ("That username could not be found!");
}
while(mysql_fetch_array($userquery, MYSQL_ASSOC)){
$username = $row['username'];
$email = $row['email'];
}
} else die ("you need to specify a username!");
?>
I'm having problems specially with the "if (mysql_num_rows($userquery) != 1){
die ("That username could not be found!");" part.
I know what is it but I cannot find a code similar to "mysql_num_rows();"
Thanks.
1 Answer
- rbjollyLv 58 years agoFavorite Answer
You could try to use the PDO rowcount() function, but its use is not reliable for counting records returned using a SELECT statement (only for UPDATE, DELETE, and INSERT). However, you could use an additional SELECT statement with the COUNT function to return the number of rows, like so:
<?php
/* Setup database connection constants. */
defined('DB_SERVER') ? NULL : define('DB_SERVER', 'localhost');
defined('DB_USER') ? NULL : define('DB_USER', 'yourMysqlUser');
defined('DB_PWD') ? NULL : define('DB_PWD', 'yourMysqlPassword');
defined('DB_NAME') ? NULL : define('DB_NAME', 'yourMysqlDbName');
try {
/* Connect to db and allow PDO errors to be handled. */
$conn = new PDO('mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME, DB_USER, DB_PWD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* Get username and escape critical characters and quote the string. */
if( !isset($_GET['username']) ) die("<p>Username not set.");
$username = $conn->quote($_GET['username']);
/* Get a count of records. */
$cntSql = " SELECT COUNT(*) FROM users WHERE username = {$username} ";
$cntResult = $conn->query($cntSql);
$numRecords = $cntResult->fetchColumn();
if ($numRecords != 1) die("That username could not be found!");
echo("<p>Num Records = {$numRecords}");
/* Get user info. */
$resultSet = $conn->query(" SELECT * FROM users WHERE username = {$username} ");
foreach($resultSet as $record) {
echo("<p>User = {$record['username']}");
echo("<p>Email = {$record['email']}");
$username = $record['username'];
$email = $record['email'];
}
} catch(PDOException $e) {
die( "ERROR: " . $e->getMessage());
}
?>
Source(s): PDO rowcount() function: http://php.net/manual/en/pdostatement.rowcount.php