jQWidgets Forums

jQuery UI Widgets Forums Chart Msql > PDO

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by  Blomotom 7 years, 12 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Msql > PDO Posts
  • Msql > PDO #94895

    Blomotom
    Participant

    Hello all,
    I would change way to connect database from msql to PDO. Could you help me to convert following code ?

    <?php
    	session_start ();
    	if(empty($_SESSION['pseudo'])) 
    	{
    	// Si inexistante ou nulle, on redirige vers le formulaire de login
    	header('Location: #');
    	exit();
    	}
    		#Include the connect.php file
    	include('../../jqwidgets/connect.php');
    	include('../../inc/config.php');
    
    	#Connect to the database
    	//connection String
    	$connect = mysql_connect($hostname, $username, $password)
    	or die('Could not connect: ' . mysql_error());
    
    	$bool = mysql_select_db($database, $connect);
    	if ($bool === False){
    	   print "can't find $database";
    	}
    
    	$query="Select CC_bar.* 
    	from CC_bar*";  // CC_bar.* = >>>> CC_bar.CC_bar1, CC_bar.CC_bar2, CC_bar.CC_bar3, CC_bar.CC_bar4, CC_bar.CC_bar5
    
    	$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
    	// get data and store in a json array
    	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    		$orders[] = array(
    			'CC_bar1' => $row['CC_bar1'],
    			'CC_bar3' => $row['CC_bar3'],
    			'CC_bar4' => $row['CC_bar4'],
    			'CC_bar5' => $row['CC_bar5'],
    		  );
    	}
    	echo json_encode($orders);
    ?>

    I have do but dont work :

    <?php
    	session_start ();
    	if(empty($_SESSION['pseudo'])) 
    	{
    	// Si inexistante ou nulle, on redirige vers le formulaire de login
    	header('Location: #');
    	exit();
    	}
    		#Include the connect.php file
    	include('../../jqwidgets/connect.php');
    	include('../../inc/config.php');
    
    	#Connect to the database
    	//connection String
            $connect = new PDO('mysql:host='.$hostname, $username, $password);	
    
    	$query="Select CC_bar.* 
    	from CC_bar*";  // CC_bar.* = >>>> CC_bar.CC_bar1, CC_bar.CC_bar2, CC_bar.CC_bar3, CC_bar.CC_bar4, CC_bar.CC_bar5
    
    	$sth = $connect->prepare($query);
    	$sth->execute();
    	// $results=$sth->fetchAll(PDO::FETCH_ASSOC); // TEST1
    
    	// get data and store in a json array
    	while ($row = $sth->fetchAll(PDO::FETCH_ASSOC)) {
    		$orders[] = array(
    			'CC_bar1' => $row['CC_bar1'],
    			'CC_bar3' => $row['CC_bar3'],
    			'CC_bar4' => $row['CC_bar4'],
    			'CC_bar5' => $row['CC_bar5'],
    		  );
    	}
    	echo json_encode($orders);
    ?>

    Thanks in advance for your help.

    Msql > PDO #94903

    Stanislav
    Participant

    Hello Blomotom,

    We are sorry, but we are not very informed with this.
    You could take a look here, I think it has some useful information.

    Best Regards,
    Stanislav

    jQWidgets Team
    http://www.jqwidgets.com/

    Msql > PDO #94913

    Blomotom
    Participant

    Thank you Stanislav, i would share working code for other users :

    connect.php

    <?php
    $hostname = "";
    $database = "";
    $username = "";
    $password = "";
    ?>

    data.php

    #Connect to the database
    	$connect = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
    	$connect->exec("SET CHARACTER SET utf8");
    	$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    #Query
            $query="Select CC_bar.* from CC_bar";
            $sth = $connect->prepare($query);
    	$sth->execute();
    	$results=$sth->fetchAll(PDO::FETCH_ASSOC);
    #Loop
    	foreach ($results as $Z8row) {
            $orders[] = array(
    			'CC_bar1' => $Z8row['CC_bar1'],
    			'CC_bar3' => $Z8row['CC_bar3'],
    			'CC_bar4' => $Z8row['CC_bar4'],
    			'CC_bar5' => $Z8row['CC_bar5'],
    		  );
    	}
    #echo json data
    	echo json_encode($orders);
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.