Why when I insert into the template does not work and when instead of ". $ Row ['name']." I insert the city of Aksai, everything works

<?php require 'connect.php'; mysql_set_charset( 'utf8' ); $sql_select = "SELECT * FROM users"; $result = mysql_query($sql_select); $row = mysql_fetch_array($result); do { printf ("<script type=\"text/javascript\"> $(function(){ $('#mydiv').text('Номер телефона: ' + ($('#cp_id_626').text().indexOf('" .$row['name'] . "') != -1 ? '".$row['email'] ."' : '+87879897')); }); </script>" ); } while($row = mysql_fetch_array($result)); ?> 

  • Aha) I don’t know why it doesn’t work ( - Vitaly Nosikov
  • one
    Mother, my woman, how are you going to debug it if you have it all. Make through while. Do - immediately executes the code, and then refers to the database, so the value is not substituted by the idea. while ($ row = mysql_fetch_array ($ result)! = false) {your code} or something like that - dima buhayov
  • Yes, this is understandable, but does not make a sample differently ( - Vitaly Nosikov
  • What specifically does not work? - Akim Kelar
  • 2
    I guess I swear at $ - because there is no connection to jQuery - alexoander

3 answers 3

 <?php $db = new Mysqli('localhost', 'user', 'pass', 'db'); $sql = $db->query('SELECT * FROM users'); ?> <?php while($data = $sql->fetch_row()): ?> <script type=\"text/javascript\"> $(function(){ $('#mydiv').text('Номер телефона: ' + ($('#cp_id_626').text().indexOf('<?php echo $row['name']; ?>') != -1 ? '<?php echo $row['email']; ?>' : '+79526036525')); }); </script> <?php endwhile; ?> 

Try this

  • Selection from the database now does not - Vitaly Nosikov
  • If I add value again, it works as if the cycle was stopped - Vitaly Nosikov
  • Well, dump $ data see what is in sql. var_dump(iterator_to_array($sql->fetch_row()) before while .... @ VitaliyNosikov - Naumov
  • Parse error: syntax error, unexpected 'var_dump' (T_STRING), expecting while (T_WHILE) - Vitaly Nosikov
  • This code makes only the first choice, how to make it do all the other samples? - Vitaly Nosikov

It is probably better to replace double quotes with single quotes, in print: print ("test") => print ('test')

as the compiler may swear at $ from jQuery as it was written above.

    I suppose I swear at $, because there is no jQuery connection.

    UPDATE

    There are more questions about what is written in the code. After all, by what is written, you will display the result of only the last element. the text will be many times overwritten and + to this you will have n + <script></script> blocks. I would suggest putting the script blocks out of the loop, and inside the loop, first make a large line, divided for example \n between entries. And at the end assign it to $("#mytext").html(result);

    UPDATE2

     <?php require 'connect.php'; mysql_set_charset( 'utf8' ); $sql_select = "SELECT * FROM users"; $result = mysql_query($sql_select); $row = mysql_fetch_array($result); // that string we will printf out $resultStr = "<script type=\"text/javascript\"> $(function(){ $('#mydiv').text(\" "; do { $resultStr+="Номер телефона: ".$row['name'].",Email: ".$row['email']." \n"; //Проверку я не добавлял - это уже сам =) } while($row = mysql_fetch_array($result)); $resultStr+=");}); </script>"; printf($resultStr); ?> 

    Sorry for my crooked code, I’m not phpshnik once, but somehow it will work a bit easier (it is desirable that #mydiv be as large as possible or even textarea at all - then it’s possible not to insert through .text, but through html)

    • The strangest thing is that if you insert directly through the console, everything works - Vitaly Nosikov
    • And this is not how jQuery (function () {jQuery ('# mydiv'). Text ('Phone Number:' + (jQuery ('# cp_id_626'). Text (). IndexOf ('". $ Row [' name ']. "')! = -1? '". $ Row [' email ']. "': '+ 7-951-528-49-30'));}); - Vitaly Nosikov
    • Changing $ to jQuery will not give you anything if you do not have access to the jQuery library inside this script. Try to add this script directly to js on the site (namely js - delete parts from $ row) - check its work. Besides, I added the answer - mb will help. - alexoander
    • Yes, that's right, performs only the last entry in the database, and can the correct code? - Vitaly Nosikov
    • Well, I would do something like that - alexoander