Solved

php debugging, parsing commer value getting variable numbers

Posted on 2014-04-05
20
295 Views
Last Modified: 2014-04-10
i have in database value that refers to ids of a another table it  consists of numbers separates by' ,'
i want to get rid of the' ,' and get each différent numbers in variables
in my code i use
  $mots_clefs = $row['mots_clefs'];
but i have
Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\citations\affiche_citations.php on line 12

what can i do?
please help

CREATE TABLE IF NOT EXISTS `citations` (
  `id` int(11) unsigned NOT NULL,
  `auteur` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `texte` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `mots_clefs` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Contenu de la table `citations`
--

INSERT INTO `citations` (`id`, `auteur`, `texte`, `mots_clefs`) VALUES
(1, '1', 'À elle seule,La vie est une citation.', '10,11'),
(0, '1', 'À elle seule,La vie est une citation.', '37,38,39');

Open in new window


php


$query1 = "SELECT * FROM citations";
$result1 = mysql_query($query1) or die(mysql_error());
while($row = mysql_fetch_array($result1))
  {
  $id=$row['auteur'];
  $mots_clefs = $row['mots_clefs'];  
 
  $mots_clefs2 = implode(',', $mots_clefs);
 
  $query2 = "SELECT NOM FROM auteurs where id=$id";
  $result2 = mysql_query($query2) or die(mysql_error());
  $row2 = mysql_fetch_array($result2);


   $auteur= $row2[0];
   $texte = $row['texte'];
  $mots_clefs = $row['mots_clefs'];  
      echo $texte ;
      echo "<strong>";
      echo  $auteur;
      echo "</strong>";
      echo $mots_clefs;  
echo"</br>";
0
Comment
Question by:jerrrrry
  • 8
  • 7
  • 5
20 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39980410
$mots_clefs = $row['mots_clefs'];
This means $mots_clefs is a string, not an array. implode expects an array.

I don't really see where you use  $mots_clefs2. You set it as an erroneous value, then forget about it.

PS: Ray will be here momentarily to tell you to switch from mysql to mysqli or PDO.

HTH,
Dan
0
 

Author Comment

by:jerrrrry
ID: 39980414
so, what can i write instead?
thanks
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39980415
You probably wanted something like:
$query2 = "SELECT NOM FROM auteurs where id IN ($mots_clefs)";
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39980422
This should work, but it's ugly and screams for refactoring. Plus it's missing the connection to the keywords table, you're only outputting the id's of the keywords.

$query1 = "SELECT * FROM citations";
$result1 = mysql_query($query1) or die(mysql_error());
while($row = mysql_fetch_array($result1))
  {
  $auteur_id=$row['auteur'];
  $mots_clefs = $row['mots_clefs'];  
   
  $query2 = "SELECT NOM FROM auteurs where id = $auteur_id";
  $result2 = mysql_query($query2) or die(mysql_error());
  $row2 = mysql_fetch_array($result2);

   $auteur= $row2[0];
   $texte = $row['texte'];
 
      echo $texte ;
      echo "<strong>";
      echo  $auteur; 
      echo "</strong>";
      echo $mots_clefs;   
echo"</br>";
}

Open in new window

0
 

Author Comment

by:jerrrrry
ID: 39980424
infact i will make a third query getting info in a mots_clefs table
but i need to get the $mots_clefs data  without the , and each one alone

$query3 = "SELECT  NOM FROM `mots_clefs` where id='$motclef'";

CREATE TABLE IF NOT EXISTS `mots_clefs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Nom` varchar(80) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;

--
-- Contenu de la table `mots_clefs`
--

INSERT INTO `mots_clefs` (`id`, `Nom`) VALUES
(1, 'vie'),
(33, ' test2'),
(34, 'test3'),
(35, 'test1'),
(36, 'test2'),
(37, 'test1'),
(38, 'test2'),
(39, 'test3');

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39980425
Good news.  Even if you are new to PHP, you can still look up every PHP function on the PHP.net web site.  Examples include these functions.  You must read and understand this stuff if you want to write PHP code, no excuses.  If your script spits out a message, the first line of defense is to look up the function and make sure your script is using it in a way that makes sense.
http://php.net/implode
http://php.net/explode

And Dan's right.  You're going to need to switch from MySQL to MySQLi or PDO.  Better to do it now on your own schedule than wait and find your scripts spitting out deprecated messages.
0
 

Author Comment

by:jerrrrry
ID: 39980430
$pizza  = "piece1,piece2,piece3,piece4,piece5,piece6";
 $pieces = explode(",", $mots_clefs);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

is it possible to tell php to consider  this string like   $mots_clefs = $row['mots_clefs'];

(that is '37,38,39') to a array?
thanks?
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39980432
Here's a still ugly code that should work with all 3 tables (and only 2 queries):
$query1 = "SELECT citations.id, auteur, texte, mots_clefs, nom 
           FROM citations LEFT JOIN auteurs ON citation.auteur = auteur.id";
$result1 = mysql_query($query1) or die(mysql_error());
while($row = mysql_fetch_array($result1)) {
    $auteur = $row['nom'];
    $texte  = $row['texte'];
    $mots_clefs_ids =  $row['mots_clefs'];
    $mots_clefs = array();
    
    $query2 = "SELECT nom FROM mots_clefs WHERE id IN ($mots_clefs_ids)";
    $result2 = mysql_query($query2) or die(mysql_error());
    while ($row2 = mysql_fetch_array($result2);) {
        $mots_clefs[] = $row2['nom'];
    };
    $mots_clefs = implode(',', $mots_clefs);
    echo "$texte<strong>$auteur</strong>$mots_clefs<br />";
}

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39980440
i need to get the $mots_clefs data  without the , and each one alone
No, you don't. You need that data to stay just the way it is, so you can use it in the IN clause:
SELECT nom FROM mots_clefs WHERE id IN (37,38,39)

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39980545
Really, you gotta look this stuff up and try to get an understanding.  Here is a code fragment from an earlier post annotated with comments.

/**
 * IN THE FOLLOWING INSTRUCTION WE WILL TAKE A LITERAL STRING
 * AND ASSIGN IT TO A VARIABLE NAMED $pizza
 */
$pizza  = "piece1,piece2,piece3,piece4,piece5,piece6";

/**
 * IN THE FOLLOWING INSTRUCTION WE WILL TAKE AN UNDEFINED VARIABLE
 * AND USE IT AS AN ARGUMENT TO A FUNCTION CALL.  $pizza IS NOT THE SAME
 * VARIABLE AS $mots_clefs AND $mots_clefs IS UNDEFINED, SO explode() WILL
 * ISSUE A NOTICE AND THE VALUE ASSIGNED TO $pieces WILL BE MEANINGLESS
 */
 $pieces = explode(",", $mots_clefs);

Open in new window

0
 

Author Comment

by:jerrrrry
ID: 39981157
sorry i made a mistake with $pizza
this is better  $pieces = explode(",", $pizza);

i tried this
<?php
require_once ("fonctions/connec.php");
$query1 = "SELECT citations.id, auteur, texte, mots_clefs, nom 
           FROM citations LEFT JOIN auteurs ON citation.auteur = auteur.id";
$result1 = mysql_query($query1) or die(mysql_error());
while($row = mysql_fetch_array($result1)) {
    $auteur = $row['nom'];
    $texte  = $row['texte'];
    $mots_clefs_ids =  $row['mots_clefs'];
   $mots_clefs = array();
    
    $query2 = "SELECT Nom FROM mots_clefs WHERE id IN ($mots_clefs_ids)";
    $result2 = mysql_query($query2) or die(mysql_error());
    while ($row2 = mysql_fetch_array($result2);) {
        $mots_clefs[] = $row2['nom'];
    };
   
    echo "$texte<strong>$auteur</strong>$mots_clefs<br />";
}


?>

Open in new window


but i get this
Parse error: parse error in C:\wamp\www\citations\affiche_citations.php on line 14 ????
thanks for helping me
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39981193
In this line 14:

while ($row2 = mysql_fetch_array($result2);) {

The semi-colon is out of place.  Omit it.

And really, get yourself some quality time with this book.  You can spend a month here or a year of trial and error.  Save your time!
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0321833899
0
 

Author Comment

by:jerrrrry
ID: 39981499
i laughed a bit because i will have to wait a while for the book...
there was a missing s on auteur so i add it but a don't have what i want still....
require_once ("fonctions/connec.php");
$query1 = "SELECT citations.id, auteur, texte, mots_clefs
           FROM citations LEFT JOIN auteurs ON citations.auteur = auteurs.id";
$result1 = mysql_query($query1) or die(mysql_error());
while($row = mysql_fetch_array($result1)) {
    $auteur = $row['auteur'];
    $texte  = $row['texte'];
    $mots_clefs_ids =  $row['mots_clefs'];
   $mots_clefs = array();
    
    $query2 = "SELECT Nom FROM mots_clefs WHERE id IN ($mots_clefs_ids)";
    $result2 = mysql_query($query2) or die(mysql_error());
    while ($row2 = mysql_fetch_array($result2)) {
        $mots_clefs[] = $row2['nom'];
    };
   
    echo "$texte<strong>$auteur</strong>$mots_clefs<br />";
}

Open in new window


i have this error
Notice: Undefined index: nom in C:\wamp\www\citations\affiche_citations.php on line 15

Notice: Undefined index: nom in C:\wamp\www\citations\affiche_citations.php on line 15
¿ elle seule,La vie est une citation.1Array

Notice: Undefined index: nom in C:\wamp\www\citations\affiche_citations.php on line 15

Notice: Undefined index: nom in C:\wamp\www\citations\affiche_citations.php on line 15

Notice: Undefined index: nom in C:\wamp\www\citations\affiche_citations.php on line 15
¿ elle seule,La vie est une citation.1Array
sql.txt
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39981513
This Nom is capitalized:

$query2 = "SELECT Nom FROM mots_clefs WHERE id IN ($mots_clefs_ids)";

This nom is not capitalized:

$mots_clefs[] = $row2['nom'];

In PHP, variables are case-sensitive.  So are table and column names in MySQL.  These are the sorts of things that you'll learn when you start some structured study of PHP and MySQL.  You will be amazed how quickly the stumbling blocks can turn into stepping stones as you start the practice exercises in a well-developed curriculum.

Anyway, we seem to be wandering now.  Have we answered your original question?
0
 

Author Comment

by:jerrrrry
ID: 39981529
no you have not answered yet the question, because i don't have the correct answer
i should have¿ elle seule,La vie est une citation. Jorge Luis BORGES keyword1 keyword2
       ¿  elle seule,La vie est une citation. Jorge Luis BORGES test1 test2
i have
¿ elle seule,La vie est une citation.1Array
¿ elle seule,La vie est une citation.1Array



i know there is accent issue we talked about with latin characters, not a problem here now i ll fix it later
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39981531
You forgot this line:
$mots_clefs = implode(',', $mots_clefs);

Open in new window

Without it you're echoing an array instead of a string...
0
 

Author Comment

by:jerrrrry
ID: 39981548
then i have this
¿ elle seule,La vie est une citation.Jorge Luis BORGESArray
¿ elle seule,La vie est une citation.Jorge Luis BORGESArray
?
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 400 total points
ID: 39981561
There's something fishy going on. Try this:
require_once ("fonctions/connec.php");
$query1 = "SELECT texte, mots_clefs, nom
           FROM citations LEFT JOIN auteurs ON citations.auteur = auteurs.id";
$result1 = mysql_query($query1) or die(mysql_error());
while($row = mysql_fetch_array($result1)) {
    $auteur = $row['nom'];
    $texte  = $row['texte'];
    $mots_clefs_ids =  $row['mots_clefs'];
    $mots_clefs = array();
    
    $query2 = "SELECT nom FROM mots_clefs WHERE id IN ($mots_clefs_ids)";
    $result2 = mysql_query($query2) or die(mysql_error());
    while ($row2 = mysql_fetch_array($result2)) {
        $mots_clefs[] = $row2['nom'];
    };
    $mots_clefs = implode(',', $mots_clefs);
   
    echo "$texte<strong>$auteur</strong>$mots_clefs<br />";
}

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 39981565
Hopefully by now you understand why you do not want more than one data element in any column.  All of this is unnecessary and confusing if the data base tables are designed correctly.

Here is an annotated bit of code explaining line-by-line what's going on.  I'll sign off now.  Best of luck with your project and please, please give yourself some much-needed quality time with that book!

/**
 * Using the while() iterator, retrieve data from the $result2 resource
 * with the fetch_array() function (which retrieves twice as much data as
 * you need, making it the least efficient way to retrieve the data). A
 * better choice would be fetch_object().
 *
 * Each iteration will load data into an array named $row2, then the next
 * instruction will be executed, inside the control structure.
 */
while ($row2 = mysql_fetch_array($result2)) {

/**
 * As each $row2 array is created, go into the array and find the value 
 * of the variable at the index named 'nom' and copy this value into an
 * array named $mots_clefs.  Because of the empty square bracket notation 
 * after $mots_clefs PHP knows (1) this is an array and (2) the variable
 * from $row['nom'] is to be appended to the array in a new numbered element.
 */
    $mots_clefs[] = $row2['nom'];

/**
 * This is the end of the control structure created by the while() iterator.
 * The semicolon is superfluous and should be removed to avoid confusion.
 */
};

/**
 * This echo statement writes the contents of three variables and some
 * string literals to the browser output stream.  Because some of the 
 * variables are strings the contents of the variable will be sent.  But 
 * PHP does not know what you want to print when you use an array with 
 * echo, so it just prints the word "Array" and ignores the data.
 */
echo "$texte<strong>$auteur</strong>$mots_clefs<br />";

/**
 * This is likely what you want just before the echo statement.  It will 
 * take the $mots_clefs array and turn it into a string variable, with each 
 * of the elements of the array separated by commas in the resulting string.
 * PHP will overwrite the $mots_clefs array variable with the new string.
 */
$mots_clefs = implode(',', $mots_clefs);

Open in new window

0
 

Author Closing Comment

by:jerrrrry
ID: 39992796
100points for ray for the explaination
400 points for the solution

now there is still http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28387674.html to achieve
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

820 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