¿Por qué me sale el error “Call to undefined function mysql_connect()”?

Question:

When I want to generate a PDF with php and MySQL I get this error, which I don't know how to solve:

Fatal error: Uncaught Error: Call to undefined function mysql_connect () in C: \ xampp \ htdocs \ responsivecode \ newappfishcode \ opnpdf \ report3_pdf.php: 4 Stack trace: # 0 {main} thrown in

Answer:

Mysql_connect is deprecated, you have to use mysqli instead:

//Creas una variable de tipo objeto mysqli con los datos de la bd y el charset que quieras
$mysqli = new mysqli('127.0.0.1', 'root', 'root', 'basededatos1');
$mysqli->set_charset("utf8");

And do the queries like this:

$res = $mysqli->query("SELECT * FROM personas");

while($f = $res->fetch_object()){
    echo $f->nombre.' <br/>';
}

Using the Query method that has the mysqli object you put the SQL string and with fetch_object put the data of each row in a new $f object to be able to access its columns in the way that I show in the example.

All the best.

Scroll to Top