Solved

please convert my php old fashion  to mysqli or PDO

Posted on 2015-02-04
16
53 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
16 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 400 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 400 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
 

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 400 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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 400 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Easy to use inventory management software 7 69
<? versus <?php 5 36
RDBMS and No sql database 4 42
datetime in sql 6 24
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now