?
Solved

please convert my php old fashion  to mysqli or PDO

Posted on 2015-02-04
16
Medium Priority
?
73 Views
Last Modified: 2016-05-27
hello i try to convert my old fashion warning php to smtg proper



connect.php

<?php // Hote

$dbhost		="localhost";
// Nom d'utilisateur
$dblogin	="root"; 
// Password utilisateur
$dbpasspord	="";
// Nom de votre base de données
$database	="portfolio"; 
// Password administrateur pour la zone d'administration
$passadmin	="admin"; 

//if I put the following line without comment the aff_image.php script doesnt work anymore
// $link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link)); 



mysql_connect($dbhost,$dblogin,$dbpasspord) or die("Impossible de se connecter à la base de données !"); 
mysql_select_db("$database") or die("Impossible de selectionner la base !"); 
mysql_query("SET NAMES 'utf8'"); 

Open in new window

<?php include('connec.php');





function affiche($src,$lang,$appel_fnc,$type,$limite){



$nombre = 5;  /* on on va afficher 5 résultats par page*/

if ($lang == "fr") {$infos="plus d'infos";}

else $infos="more informations";

              



$requete = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.' ') OR die('Erreur de la requête images');

$ntotal = mysql_num_rows($requete);







$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

     { 

    $sClassName = 'imagette';

    if($iCpt === 1) {$sClassName = 'imagette2'; }

    ++$iCpt;

    echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:enlarge(this)" border="0" alt="'.$resultat['chemin'].'"  /></a>';

     }



$limitesuivante = $limite + $nombre;

$limiteprecedente = $limite - $nombre;



   echo '</div> <div id="barre_defil">';

switch ($appel_fnc) {



 /* on on va afficher 5 résultats par page*/

    case 1:

    

        if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limiteprecedente.'/0/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}



if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limitesuivante.'/0/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 2:

          if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limiteprecedente.'/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limitesuivante.'/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 3:

   if($limite != 0) {

    echo '<a href="/'.$src.'/'.$limiteprecedente.'"><img src="/fnc/prev.gif" border="0" alt="precedent" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="appel8.php?limite='.$limitesuivante.'"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}   

   break;     

}   





   echo ' </div> ';

}







?>

Open in new window

0
Comment
Question by:jerrrrry
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 1600 total points
ID: 40589211
OK, I'm working from this documentation.

$link = new mysqli("myhost","myuser","mypassw","mybd");
if ($link->connect_errno)  die("Error " . $link->connect_errno );

$link->query("SET NAMES 'utf8'"); 

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40589214
I can suggest you to read this: http://www.sitepoint.com/migrate-from-the-mysql-extension-to-pdo/ It can help you to migrate your code to PDO, the new de facto standard layer for myql.
0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 1600 total points
ID: 40589277
For your 2nd code block, I assume you want a parameterized query to help you avoid SQL injection.  That's one of the major draws of mysqli.

So ... lines 21-23 translate like this:

$stmt = $link->prepare('SELECT *  FROM image WHERE s_cat=  ?');
$stmt->bind_param("s", $type);
$stmt->execute();

$stmt->store_results();
$ntotal = $stmt->$stmt->num_rows;

Open in new window


http://php.net/manual/en/mysqli.prepare.php
http://php.net/manual/en/mysqli-result.num-rows.php
http://php.net/manual/en/mysqli-stmt.execute.php
http://foundationphp.com/blog/2011/09/24/using-num_rows-with-a-mysqli-prepared-statement/
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jerrrrry
ID: 40589387
how can i translate this?
$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

Open in new window



tks
$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

Open in new window

$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

Open in new window

0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 1600 total points
ID: 40589460
http://php.net/manual/en/mysqli-stmt.bind-param.php
http://php.net/manual/en/mysqli-result.fetch-assoc.php

$requete2 = $link->prepare('SELECT *  FROM image WHERE s_cat= ? limit ?, ?');
$requete2->bind_param("sdd", $type, $limite, $nombre);
$requete2->execute();
$requete2->store_results();

$iCpt = 1;

while ($resultat = $requete2->fetch_assoc()){

}

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 40590830
There is an article here at E-E that addresses exactly this issue.  It maps the familiar but obsolete MySQL extension to the currently supported versions.  Please read it over and post back here if you still have any questions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:jerrrrry
ID: 40590978
hello i have little problem...

) Notice: Undefined variable: link in C:\wamp\www\aff_image.php on line 18
Call Stack
#      Time      Memory      Function      Location
1      0.0000      145552      {main}( )      ..\web.php:0
2      0.0100      166384      affiche( )      ..\web.php:229

( ! ) Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\aff_image.php on line 18
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40590997
That is a GREAT article, Ray!
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40591000
Jerrrrry, please post the code at least through line 18 that breaks.
0
 

Author Comment

by:jerrrrry
ID: 40591011
<?php include('connec.php');





function affiche($src,$lang,$appel_fnc,$type,$limite){



$nombre = 5;  /* on on va afficher 5 résultats par page*/

if ($lang == "fr") {$infos="plus d'infos";}

else $infos="more informations";

              
$stmt = $link->prepare('SELECT *  FROM image WHERE s_cat=  ?');
$stmt->bind_param("s", $type);
$stmt->execute();

$stmt->store_results();
$ntotal = $stmt->$stmt->num_rows;


 





$requete2 = $link->prepare('SELECT *  FROM image WHERE s_cat= ? limit ?, ?');
$requete2->bind_param("sdd", $type, $limite, $nombre);
$requete2->execute();
$requete2->store_results();

$iCpt = 1;

while ($resultat = $requete2->fetch_assoc()){




     

    $sClassName = 'imagette';

    if($iCpt === 1) {$sClassName = 'imagette2'; }

    ++$iCpt;

    echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:enlarge(this)" border="0" alt="'.$resultat['chemin'].'"  /></a>';

     }



$limitesuivante = $limite + $nombre;

$limiteprecedente = $limite - $nombre;



   echo '</div> <div id="barre_defil">';

switch ($appel_fnc) {



 /* on on va afficher 5 résultats par page*/

    case 1:

    

        if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limiteprecedente.'/0/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}



if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limitesuivante.'/0/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 2:

          if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limiteprecedente.'/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limitesuivante.'/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 3:

   if($limite != 0) {

    echo '<a href="/'.$src.'/'.$limiteprecedente.'"><img src="/fnc/prev.gif" border="0" alt="precedent" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="appel8.php?limite='.$limitesuivante.'"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}   

   break;     

}   





   echo ' </div> ';

}







?>

Open in new window

connec.php
//$dbhost		="localhost";
// Nom d'utilisateur
//$dblogin	="root"; 
// Password utilisateur
//$dbpasspord	="";
// Nom de votre base de données
//$database	="portfolio"; 
// Password administrateur pour la zone d'administration
//$passadmin	="admin"; 

//if I put the following line without comment the aff_image.php script doesnt work anymore
 $link = mysqli_connect("localhost","root","","portfolio") or die("Error " . mysqli_error($link)); 



//mysql_connect($dbhost,$dblogin,$dbpasspord) or die("Impossible de se connecter à la base de données !"); 
//mysql_select_db("$database") or die("Impossible de selectionner la base !"); 

//mysql_query("SET NAMES 'utf8'"); 

    

?>

Open in new window

thanks
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40592117
Incorporate the first block of code I gave you to get your connection.

Have you read the article Ray posted?  It really helps!
0
 

Author Comment

by:jerrrrry
ID: 40592238
hello i try with what u gave me but it doesn't work....
there is a lot to read i haven't finished
thanks
0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 1600 total points
ID: 40592340
OK, you're trying to use $link as a global connection, right?  But in your function, $link isn't defined.  

Somewhere between lines 7 and 18 put:


global $link;

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40592701
@Daniel, thanks!
0
 

Author Comment

by:jerrrrry
ID: 40593015
ok now i have  Fatal error: Call to undefined method mysqli_stmt::store_results() in C:\wamp\www\aff_image.php on line 22

<?php include('connec.php');





function affiche($src,$lang,$appel_fnc,$type,$limite){

global $link;

$nombre = 5;  /* on on va afficher 5 résultats par page*/

if ($lang == "fr") {$infos="plus d'infos";}

else $infos="more informations";

              
$stmt = $link->prepare('SELECT *  FROM image WHERE s_cat=  ?');
$stmt->bind_param("s", $type);
$stmt->execute();

$stmt->store_results();
$ntotal = $stmt->$stmt->num_rows;


 





$requete2 = $link->prepare('SELECT *  FROM image WHERE s_cat= ? limit ?, ?');
$requete2->bind_param("sdd", $type, $limite, $nombre);
$requete2->execute();
$requete2->store_results();

$iCpt = 1;

while ($resultat = $requete2->fetch_assoc()){




     

    $sClassName = 'imagette';

    if($iCpt === 1) {$sClassName = 'imagette2'; }

    ++$iCpt;

    echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:enlarge(this)" border="0" alt="'.$resultat['chemin'].'"  /></a>';

     }



$limitesuivante = $limite + $nombre;

$limiteprecedente = $limite - $nombre;



   echo '</div> <div id="barre_defil">';

switch ($appel_fnc) {



 /* on on va afficher 5 résultats par page*/

    case 1:

    

        if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limiteprecedente.'/0/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}



if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limitesuivante.'/0/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 2:

          if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limiteprecedente.'/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limitesuivante.'/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 3:

   if($limite != 0) {

    echo '<a href="/'.$src.'/'.$limiteprecedente.'"><img src="/fnc/prev.gif" border="0" alt="precedent" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="appel8.php?limite='.$limitesuivante.'"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}   

   break;     

}   





   echo ' </div> ';

}







?>

Open in new window

thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question