0

Can you figure out my code? All the code does is: No database selected It won't get the data from the db. The server os is Ubuntu or OS X. I been pulling my hair out for hours.

<?php
mysqli_connect("localhost", "root", "");
mysql_select_db("hit-counter");

$sql_get_count = mysql_query("SELECT id FROM hit_info ORDER BY id DESC LIMIT 1");

if($sql_get_count === FALSE) { 
    die(mysql_error());
}
while($row = mysql_fetch_assoc($sql_get_count)) { 
    print_r($row);
} 
?>

I try this, it does the same

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("hit-counter");

$sql_get_count = mysql_query("SELECT id FROM hit_info ORDER BY id DESC LIMIT 1");

if($sql_get_count === FALSE) { 
    die(mysql_error());
}
while($row = mysql_fetch_assoc($sql_get_count)) { 
    print_r($row);
} 
?>
Tate A
  • 1
  • 4

4 Answers4

3

You didn't mention database name:try this

      <?php
  $con = mysqli_connect("127.0.0.1","root","654321","testV2") or die("Some error occurred during connection " . mysqli_error($con));

  // Write query

  $strSQL = "SELECT id FROM did  ORDER BY id DESC LIMIT 1";

  // Execute the query.

  $query = mysqli_query($con, $strSQL);
  while($result = mysqli_fetch_array($query))
  {
echo $result["id"]."
  ";
  }

  // Close the connection
  mysqli_close($con);
  ?>
soni8010
  • 385
  • 1
  • 6
  • 19
3

you have an error in your code. You use mysqli_ function to connect the server but you use a deprecated function mysql_ to select the database.

Try this code:

mysqli_connect("localhost", "root", "");
mysqli_select_db("hit-counter");

Another option when using mysqli_ is to select the database you want during connecting to the server:

$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
Qrzysio
  • 1,147
  • 3
  • 12
  • 25
  • 1
    Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /Applications/XAMPP/xamppfiles/htdocs/g.php on line 3 Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/XAMPP/xamppfiles/htdocs/g.php on line 5 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/g.php on line 8 – Tate A Mar 11 '16 at 07:51
  • `$db = mysqli_connect("localhost", "root", ""); mysqli_select_db("hit-counter", $db);` – Qrzysio Mar 11 '16 at 07:54
  • `Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /Applications/XAMPP/xamppfiles/htdocs/g.php on line 3` No database selected – Tate A Mar 11 '16 at 08:03
  • Follow my comment below about `mysql_` and `mysqli_` functions. You have to notice which one you use. – Qrzysio Mar 11 '16 at 08:04
  • sorry, you figure it out! thanks man :) – Tate A Mar 11 '16 at 08:08
  • 2
    In `mysqli_`, the connection comes first, not last `mysqli_select_db($db, "hit-counter");` – Funk Forty Niner Mar 11 '16 at 14:01
1

You cannot interchange the mysql and mysqli functions, please modify your mysql_select_db to mysqli_select_db.

William Madede
  • 727
  • 4
  • 8
0

I will not go over the errors everyone else has pointed out. But, I will mention one that no one has. I think the - character in your database name will also cause problems. You should enclose the database name in back ticks. The back tick is this ` character, most likely the far left key above the TAB key. If you had error reporting turned on, or looked at your php error log, you would have seen the error.

CharlesEF
  • 608
  • 1
  • 15
  • 25