0

I rarely do programming. I only know enough to be dangerous as they say and I simply assemble bits of code to get what I want. My code below seems to die at the $sql query statement. It never returns any data. It should show the 13 records that are present, but it says there is none to return. I'm guessing this is some kind of syntax error?

<?php
$host = 'myipaddress';
$user = 'myuser';
$pass = 'mypass';
$db = 'mydatabase';

$conn = mysql_connect($host, $user, $pass, $db) or die("Can not connect." . mysql_error());

// Create connection
//$conn = mysqli_connect($host, $user, $pass, $db);
// Check connection
if (!$conn) {
die("Connection failed: ");
}

$sql = "SELECT * FROM pages WHERE pid > '5'";
$result = mysql_query($conn, $sql);

if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["pid"]. " - Name: " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}

mysql_close($conn);
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • this `$conn = mysql_connect($host, $user, $pass, $db)` doesn't do what you think it does. RTM http://php.net/manual/en/function.mysql-connect.php - same for `$result = mysql_query($conn, $sql);` Again, RTM http://php.net/manual/en/function.mysql-query.php – Funk Forty Niner Dec 30 '15 at 15:09
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 30 '15 at 15:09
  • 1
    You're mixing `mysql_*` and `mysqli_*` functions. That won't work. – Jay Blanchard Dec 30 '15 at 15:10
  • *Ah, that too Sam* - @JayBlanchard amongst other things. – Funk Forty Niner Dec 30 '15 at 15:10
  • I have no idea why your question was downvoted, we all have to start learning somewhere. My advice here. Research before you try to code, mysqli and pdo are the preferred methods for database access now as they are more secure. Learning both at once may make your life difficult, so choose one and stick with it until you have it covered then learn the other if you feel it will benefit you. Ive corrected the down vote as i dont think it was justified. – CodingInTheUK Dec 30 '15 at 15:38

3 Answers3

0

Your using the mysql_ API right up until you try to fetch rows here, where you're using mysqli_. That will not work.

while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["pid"]. " - Name: " . $row["title"]. "<br>";
}

Your script is at risk for SQL Injection Attacks. Please stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.

EDIT: Your connection (Good Eyes Ralph!) string will not work because mysql_connect() doesn't accept the database as part of the connection. You must use the additional function mysql_select_db() to choose your database.

In addition, it is not necessary to specify the connection link in mysql_query() but if you do it should be the second argument:

$result = mysql_query($sql, $conn);
Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
0

You need to use mysql_fetch_assoc() in place of mysqli_fetch_assoc(), because your previous functions are based on mysql_*, not mysqli_*

if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysql_fetch_assoc($result)) {
    echo "id: " . $row["pid"]. " - Name: " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}
Prabu Guna
  • 344
  • 1
  • 3
  • 14
0

There is quite a bit wrong with your code.

mysql_connect($host, $user, $pass, $db)

mysql_connect() uses 3 parameters, the 4th doesn't do what you think it does.

You need to use mysql_select_db() http://php.net/manual/en/function.mysql-select-db.php

Then,

$result = mysql_query($conn, $sql);

The connection comes second in mysql_.

Then you're mixing a MySQLi function mysqli_fetch_assoc which doesn't intermix with the mysql_ library.

So, just use the full MySQLi library

or PDO:

Along with a prepared statement:


Check for the real errors, should your query fail:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


As you can see, I did not provide you with a full rewrite, as I feel that in "Teaching a person how to fish...", will feed them for life, rather than "Throwing them a fish...", and only feed them for a day (wink).

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141