-1

I am trying to learn how to create a CRUD tool to get my first taste of playing with MYSQL. I am using the following tutorial: http://trendinquirer.com/simple-crud-in-php-add-edit-delete-view-in-php/

Building on WAMP server locally.

I have all the functions working however every page is giving me MYSQL deprecation warnings.

I am very new to this and wondering if anyone can give me any pointers as I seem to be too new to make sense of the PHP manual.

When I change:

<?php
mysql_connect('localhost','username','password'); /** Syntax ==> mysql_connect(SERVERNAME,USERNAME,PASSWORD); **/   
mysql_select_db('development_logs'); /** This will select the databasename **/

to:

$connection = mysqli_connect('localhost','username','password');
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}

// 2. Select a database to use 
$db_select = mysqli_select_db($connection, 'development_logs');
if (!$db_select) {
die("Database selection failed: " . mysqli_error());
}

I then get a whole new set of errors corresponding to my index.php page around this string:

<?php
$result = mysql_query("SELECT * FROM website_logs");
while($data = mysql_fetch_object($result) ):
?>

Any help on at least getting past this point would be hugely appreciated.

mobius2000
  • 91
  • 9
  • 1
    Why is `mysql_query` going to work when you're creating a connection with `mysqli` ? – ta.speot.is Jun 28 '14 at 00:25
  • 1
    Yeah you're going to have to change `mysql_query` to `mysqli_query`, or create a mysqli object and call `mysqli::query` – SharkofMirkwood Jun 28 '14 at 00:27
  • possible duplicate of [Changing this from MySQL to MySQLi?](http://stackoverflow.com/questions/4640520/changing-this-from-mysql-to-mysqli) – FuzzyTree Jun 28 '14 at 00:32

1 Answers1

2

You need to use mysqli_connect_error() to report a problem with mysqli_connect(), because mysqli_error() requires a connection argument.

Once you open the connection, you have to use mysqli_query() and mysqli_fetch_object(). mysqli_query() requires $connection as the first argument:

$result = mysqli_query($connection, "SELECT * FROM website_logs") or die ("Query failed: " . mysqli_error($connection));
while ($data = mysqli_fetch_object($result)) {
    ...
}
Barmar
  • 669,327
  • 51
  • 454
  • 560
  • +1 This is correct but it might be worth pointing OP to [this page](https://php.net/manual/en/mysqlinfo.api.choosing.php) which discussed `mysql_*` vs `mysqli_*` and PDO (the latter I consider to be - subjectively - the best option). – ta.speot.is Jun 28 '14 at 01:59
  • Thank you, I found that much clearer than the general manual I seemed to be working with. – mobius2000 Jun 28 '14 at 09:11