-1

I cannot connect to my Data Base using this way:

mysql_connect("localhost","root","") or die("1");
mysql_select_db("database") or die("2");

After the connection to the data base I would like to create a while loop with the elements of the data base, this is my code:

$carlistreq = mysql_query("SELECT * FROM cars WHERE brand="'.$_GET['marq'].'"") or die("3");
$count = mysql_num_rows($carlistreq);

if($count==0)
{
   echo "No elements";
}
else
{
    while($row= mysql_fetch_array($query))
    {
        //my code
    }
}

The issue comes from my SQL attributes like:

mysql_connect
mysql_fetch_array
mysql_select_db

For example.

Here is the error:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:[....] on line 2

BorisD
  • 1,611
  • 17
  • 22
  • 1
    [Your question was already been answered.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) Please, read the question and answer of this link carefully. You'll gonna find every answer you're looking for. – al'ein Oct 20 '15 at 10:08
  • Here is the [`PDO`](http://php.net/manual/en/book.pdo.php) class manual, and here's the [`mysqli_`](http://php.net/manual/en/book.mysqli.php) one. Both replace your needs. They are quite simple to use, and `mysqli_` states for "MySQL Improved", which is why you're gonna find much similarity between them. If you find any trouble implementing it, please open a question concerning your issue so we can help you properly. :) – al'ein Oct 20 '15 at 10:13
  • Thank you @Tanuel Mategi solved my problem. – BorisD Oct 20 '15 at 10:18
  • Possible duplicate of [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Cœur Mar 17 '18 at 16:05

3 Answers3

0

Use PDO

$db = new PDO('mysql:host=localhost;dbname=databaseName', 'User' , 'password');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
Shridhar Patil
  • 362
  • 1
  • 17
0

use the mysqli-extension like this:

$mysqli = new mysqli("localhost","user","password","database") or die("1");
$sql = "SELECT * FROM cars WHERE brand='".$_GET['marq']."'";
$result = $mysqli->query($sql);
if($result){
    while($row = $result->fetch_assoc()){
        //do something with your $row here
    }
}

http://php.net/manual/en/book.mysqli.php

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tanuel Mategi
  • 1,253
  • 6
  • 13
0

Yes now servers are going to handel mysqli and PDO. Try implementing this code:

<?php
$con = mysqli_connect("localhost","root","");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?> 

// Change database to "test"
mysqli_select_db($con,"test");

// ...some PHP code for database "test"...

mysqli_close($con);
?> 

You can user http://php.net/manual/en/book.mysqli.php as reference to understand better how mysqli works.

Navnish Bhardwaj
  • 1,687
  • 25
  • 39