0

I have been looking for hours and i have tried multiple items from different websites but still i cant get the following code to work. Let me explain what i want to achieve. In my client databases they have a field where they can add there phonenumber. Now i want to use this phonenumber to create a click to call button. I have the following code already. And I am doing something wrong but i cant figure out what i do wrong.

//select the item from the table    
$sql = "SELECT * FROM $table WHERE id='1'";  
$resultaat = mysql_query($sql) 
  or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
//show the telephonenumber (UNCOMMENT FOR DEBUG)
while($row = mysql_fetch_array($resultaat))
  {
  echo $row['telephone'];
  echo "<br>";
  }
//make the phonenumber a variable   
$telephone = $row['telephone'];
?>
<div class="clicktocall">
<a href="tel:<?php echo $telephone ?>"><img src="contact.png"></a>
</div>

I have also tried this:

<a href="tel:<?php echo $row['telephone']; ?>"><img src="contact.png"></a>

basically the output should be like this:

<a href="tel:+1800229933">Call us free!</a>

Thanks for any answers.

From the answers I received so far I changed the code to the following.

//select the item from the table    
$sql = "SELECT * FROM $table WHERE id='1'";  
$resultaat = mysql_query($sql) 
  or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
//show the telephonenumber (UNCOMMENT FOR DEBUG)
$row = mysql_fetch_assoc($resultaat))
$telephone = $row['telephone'];
//make the phonenumber a variable   
$telephone = $row['telephone'];
?>
<div class="clicktocall">
<?php
echo '<a href="tel:'.$row['telephone'].'"><img src="contact.png"></a>';
?>
</div>
<a href="tel:+1800229933">Call us free!</a>

But it is not working the link is now tel: So it is not showing the number.

Okey it is working now the answer was given by NoLifeKing

Below the code that worked:

//select the item from the table    
$sql = "SELECT * FROM $table WHERE id='1'";  
$resultaat = mysql_query($sql) 
  or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
//show the telephonenumber (UNCOMMENT FOR DEBUG)
$row = mysql_fetch_array($resultaat);
//make the phonenumber a variable   
$telephone = $row['telephone'];
?>
<div class="clicktocall">
<a href="tel:<?php echo $telephone ?>"><img src="contact.png"></a>
</div>

4 Answers 4

2

Replace the while-loop with $row = mysql_fetch_array($resultaat);

Since you don't have more rows you don't need to loop it all.

Finished code:

//select the item from the table    
$sql = "SELECT * FROM $table WHERE id='1'";  
$resultaat = mysql_query($sql) 
  or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
//show the telephonenumber (UNCOMMENT FOR DEBUG)
$row = mysql_fetch_array($resultaat);
//make the phonenumber a variable   
$telephone = $row['telephone'];
?>
<div class="clicktocall">
<a href="tel:<?php echo $telephone ?>"><img src="contact.png"></a>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

I just copied your code and put it in the file i have and it is not showing the number. I am going to try to call it with my mobile and see if it actually works. I am already very excited. I tested it and it is WORKING. Thank you so much for this answer.
Seems in your edit that you missed a ; after the mysql_fetch_array
1

if you've only got a single row from your database query, you don't need a while loop and can therefore change your code to this.

<?php
$sql = "SELECT * FROM $table WHERE id = '1'";  
$resultaat = mysql_query($sql) 
    or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
$row = mysql_fetch_assoc($resultaat);
$telephone = $row['telephone'];
?>

Then you're free to use $telephone wherever you want.

2 Comments

I receive syntax error, unexpected '$telephone' The question is now answered thank for the input
Sorry, I missed a semi-colon at the end of this line $row = mysql_fetch_assoc($resultaat); I've updated my answer
0

$telephone Move into WHILE tag

while($row = mysql_fetch_array($resultaat))
{
   echo $telephone = $row['telephone']."<br />";
}

1 Comment

Thank you for your input the question is already answered by another user. Appreciate it!
0

You can echo the <a> tag in PHP:

while($row = mysql_fetch_array($resultaat))
{
   echo '<a href="tel:'.$row['telephone'].'"><img src="contact.png"></a>';
}

3 Comments

I have tried your suggestion but the output is only tel: it is not showing the number
Perhaps you need to put the thing in a loop
Thank you for the suggestion the question is been answered by another user. But I really appreciate the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.